/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.quickorm.dialect;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 *
 * @author aaa
 */
public class SqlServerDialect extends Dialect {

    private String dBNullString = "NULL";
    private String beforeIdentifierSeparatingChar = "[";
    private String afterIdentifierSeparatingChar = "]";
    private String beforeStringSeparatingChar = "'";
    private String afterStringSeparatingChar = "'";
    private boolean supportPagedQuerySqlOptimize = true;
    private Map<String, String> replaceStringMap;

    public SqlServerDialect() {
        init();
    }

    private void init() {
        replaceStringMap = new LinkedHashMap<String, String>();
        replaceStringMap.put("'", "''");
        replaceStringMap.put("\b", "' + CHAR(8) + '");
        replaceStringMap.put("\t", "' + CHAR(9) + '");
        replaceStringMap.put("\n", "' + CHAR(10) + '");
        replaceStringMap.put("\r", "' + CHAR(13) + '");
    }

    @Override
    public String getDBNullString() {
        return dBNullString;
    }

    /**
     * 设置DBNull在SQL语句中对应的字符串
     * @param dBNullString 
     */
    public void setDBNullString(String dBNullString) {
        this.dBNullString = dBNullString;
    }

    @Override
    public String getIdentifierPrefix() {
        return beforeIdentifierSeparatingChar;
    }

    /**
     * 设置前置标识符的分隔字符串
     * @param beforeIdentifierSeparatingChar 
     */
    public void setBeforeIdentifierSeparatingChar(String beforeIdentifierSeparatingChar) {
        this.beforeIdentifierSeparatingChar = beforeIdentifierSeparatingChar;
    }

    @Override
    public String getIdentifierSuffix() {
        return afterIdentifierSeparatingChar;
    }

    /**
     * 设置后置标识符的分隔字符串
     * @param afterIdentifierSeparatingChar 
     */
    public void setAfterIdentifierSeparatingChar(String afterIdentifierSeparatingChar) {
        this.afterIdentifierSeparatingChar = afterIdentifierSeparatingChar;
    }

    @Override
    public String getValuePrefix() {
        return beforeStringSeparatingChar;
    }

    /**
     * 设置前置字符串的分隔字符串
     * @param beforeStringSeparatingChar 
     */
    public void setBeforeStringSeparatingChar(String beforeStringSeparatingChar) {
        this.beforeStringSeparatingChar = beforeStringSeparatingChar;
    }

    @Override
    public String getValueSuffix() {
        return afterStringSeparatingChar;
    }

    /**
     * 设置后置字符串的分隔字符串
     * @param afterStringSeparatingChar 
     */
    public void setAfterStringSeparatingChar(String afterStringSeparatingChar) {
        this.afterStringSeparatingChar = afterStringSeparatingChar;
    }

    @Override
    public Map<String, String> getReplaceSpecialStringMap() {
        return replaceStringMap;
    }

    @Override
    public boolean isSupportPagedQuerySqlOptimize() {
        return supportPagedQuerySqlOptimize;
    }

    public void setSupportPagedQuerySqlOptimize(boolean supportPagedQuerySqlOptimize) {
        this.supportPagedQuerySqlOptimize = supportPagedQuerySqlOptimize;
    }

    @Override
    public String getRecordCountSql(String sql) {
        return String.format("SELECT COUNT(*) FROM (%s) AS a", sql);
    }

    @Override
    public String getPagedQuerySql(String sql, int offset, int limit) {
        return String.format("SELECT a.* FROM (SELECT ROW_NUMBER() OVER (ORDER BY GETDATE()) AS ROW_NUMBER,* FROM (%s) AS a) AS a WHERE a.ROW_NUMBER >= %s AND a.ROW_NUMBER <= %s", sql, offset + 1, limit);
    }
}
